package Socket;

import com.sun.org.apache.xerces.internal.impl.xpath.XPath;

import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;

public class Socket02 {
    public static void main(String[] args) {
        String url1 = "https://tse4-mm.cn.bing.net/th/id/OIP-C.Tu0Z2bvugQIR2s_GMB9UKgHaLH?pid=ImgDet&rs=1";
        String url2 = "https://tse3-mm.cn.bing.net/th/id/OIP-C.LtOyzBm59sU1Z-a7jmUOzwHaNK?pid=ImgDet&rs=1";
        String file1 = "F:\\桌面\\java企业级开发\\TestSocket\\1.jpg";
        String file2 = "F:\\桌面\\java企业级开发\\TestSocket\\2.jpg";

        new MyThread(url1,file1).start();
        new MyThread(url2,file2).start();
    }
}

class MyThread extends Thread{

    String urlPath;
    String imgPath;

    public MyThread (String urlPath,String imgPath){
        this.urlPath = urlPath;
        this.imgPath = imgPath;
    }



    @Override
    public void run() {
        try {
            URL url = new URL(urlPath);
            URLConnection connection =  url.openConnection();
            //获取输入流
            InputStream in = connection.getInputStream();
            //输出流，写到对应位置
            FileOutputStream fileOut = new FileOutputStream(imgPath);

            //开始写
            int b = -1;
            b = in.read();
            while (b!= -1){
                fileOut.write(b);
                fileOut.flush();
                b = in.read();
            }
            System.out.println("下载完成");



        } catch (Exception e) {
            e.printStackTrace();
        }


    }
}
